package org.edu.modules.sxjy.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.edu.common.api.vo.Result;
import org.edu.modules.sxjy.entity.VeClassTeacher;
import org.edu.modules.sxjy.entity.VeCommon;
import org.edu.modules.sxjy.entity.VeCompany;
import org.edu.modules.sxjy.entity.VeCompanyUser;
import org.edu.modules.sxjy.entity.VeDynamic;
import org.edu.modules.sxjy.entity.VePractice;
import org.edu.modules.sxjy.entity.VeStudent;
import org.edu.modules.sxjy.entity.VeStudentInfo;
import org.edu.modules.sxjy.feign.VeCommonClient;
import org.edu.modules.sxjy.mapper.VeDynamicMapper;
import org.edu.modules.sxjy.service.IVeClassTeacharService;
import org.edu.modules.sxjy.service.IVeCompanyService;
import org.edu.modules.sxjy.service.IVeCompanyUserService;
import org.edu.modules.sxjy.service.IVeDynamicService;
import org.edu.modules.sxjy.service.IVePracticeService;
import org.edu.modules.sxjy.utils.DateUtils;
import org.edu.modules.sxjy.vo.VeDynamicAddVo;
import org.edu.modules.sxjy.vo.VeDynamicInfoVo;
import org.edu.modules.sxjy.vo.VeDynamicInfosVo;
import org.edu.modules.sxjy.vo.VeDynamicVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class VeDynamicServiceImpl
        extends ServiceImpl<VeDynamicMapper, VeDynamic>
        implements IVeDynamicService
{
    @Autowired
    private VeDynamicMapper veDynamicMapper;
    @Autowired
    private IVeDynamicService veDynamicService;
    @Autowired
    private IVeCompanyService veCompanyService;
    @Autowired
    private VeCommonClient veCommonClient;
    @Autowired
    private IVeCompanyUserService veCompanyUserService;
    @Autowired
    private IVeClassTeacharService veClassTeacharService;
    @Autowired
    private IVePracticeService vePracticeService;

    public List<VeDynamicVo> selectAll(VeDynamicVo veDynamicVo, Integer startLine, Integer pageSize, VeCommon veCommon)
    {
        List<VeDynamicVo> veDynamicVoList = this.veDynamicMapper.selectAll(veDynamicVo, startLine, pageSize, veCommon);
        VeCompanyUser veCompanyUser;
        if (veCommon.getUserType().contains("0"))
        {
            veDynamicVoList = this.veDynamicMapper.selectAll(veDynamicVo, startLine, pageSize, veCommon);
        }
        else if (veCommon.getUserType().equals("1"))
        {
            List<Integer> stuBjIdList = new ArrayList();

            List<VeClassTeacher> veClassTeacherList = this.veClassTeacharService.getBanjiIdByTeacherUserId(veCommon.getUserId());
            for (VeClassTeacher veClassTeacher : veClassTeacherList) {
                if ((veClassTeacher != null) && (!"".equals(veClassTeacher))) {
                    stuBjIdList.add(Integer.valueOf(veClassTeacher.getBjId()));
                }
            }
            veCommon.setStuBjId(stuBjIdList);
            if ((stuBjIdList != null) && (stuBjIdList.size() > 0)) {
                veDynamicVoList = this.veDynamicMapper.selectAll(veDynamicVo, startLine, pageSize, veCommon);
            } else {
                veDynamicVoList = new ArrayList();
            }
        }
        else if (veCommon.getUserType().equals("2"))
        {
            Object stuResult = this.veCommonClient.queryStudentById("09d5e1e7f9b049008eee645c783a1d67", veCommon.getUserId()).getResult();
            Map stuMap = (Map)stuResult;
            if (stuMap != null)
            {
                veCommon.setStuId(Integer.parseInt(stuMap.get("id").toString()));
                veDynamicVoList = this.veDynamicMapper.selectAll(veDynamicVo, startLine, pageSize, veCommon);
            }
        }
        else if ((veCommon.getUserType().equals("4")) || (veCommon.getUserType().equals("3")))
        {
            veCompanyUser = this.veCompanyUserService.getCpInfoByUserId(veCommon.getUserId());
            veCommon.setCpId(veCompanyUser.getCpId());

            veDynamicVoList = this.veDynamicMapper.selectAll(veDynamicVo, startLine, pageSize, veCommon);
        }
        if ((veDynamicVoList != null) && (veDynamicVoList.size() > 0)) {
            for (VeDynamicVo dynamicVo : veDynamicVoList) {
                if ((dynamicVo != null) && (!"".equals(dynamicVo))) {
                    if (dynamicVo.getXsdqztm().equals("XS")) {
                        dynamicVo.setXsdqztms("新生");
                    } else if (dynamicVo.getXsdqztm().equals("ZX")) {
                        dynamicVo.setXsdqztms("在校");
                    } else if (dynamicVo.getXsdqztm().equals("XX")) {
                        dynamicVo.setXsdqztms("休学");
                    } else if (dynamicVo.getXsdqztm().equals("TX")) {
                        dynamicVo.setXsdqztms("退学");
                    } else if (dynamicVo.getXsdqztm().equals("KC")) {
                        dynamicVo.setXsdqztms("开除");
                    } else if (dynamicVo.getXsdqztm().equals("BY")) {
                        dynamicVo.setXsdqztms("毕业");
                    } else if (dynamicVo.getXsdqztm().equals("YY")) {
                        dynamicVo.setXsdqztms("肄业");
                    } else if (dynamicVo.getXsdqztm().equals("ZXX")) {
                        dynamicVo.setXsdqztms("转学");
                    } else if (dynamicVo.getXsdqztm().equals("JY")) {
                        dynamicVo.setXsdqztms("结业");
                    }
                }
            }
        }
        return veDynamicVoList;
    }

    public int getSumPage(VeDynamicVo veDynamicVo, VeCommon veCommon)
    {
        int pageNum = 0;
        if (veCommon.getUserType().contains("0"))
        {
            pageNum = this.veDynamicMapper.getSumPage(veDynamicVo, veCommon);
        }
        else if (veCommon.getUserType().equals("1"))
        {
            List<Integer> stuBjIdList = new ArrayList();

            List<VeClassTeacher> veClassTeacherList = this.veClassTeacharService.getBanjiIdByTeacherUserId(veCommon.getUserId());
            for (VeClassTeacher veClassTeacher : veClassTeacherList) {
                if ((veClassTeacher != null) && (!"".equals(veClassTeacher))) {
                    stuBjIdList.add(Integer.valueOf(veClassTeacher.getBjId()));
                }
            }
            veCommon.setStuBjId(stuBjIdList);
            if ((stuBjIdList != null) && (stuBjIdList.size() > 0)) {
                pageNum = this.veDynamicMapper.getSumPage(veDynamicVo, veCommon);
            } else {
                pageNum = 0;
            }
        }
        else if (veCommon.getUserType().equals("2"))
        {
            Object stuResult = this.veCommonClient.queryStudentById("09d5e1e7f9b049008eee645c783a1d67", veCommon.getUserId()).getResult();
            Map stuMap = (Map)stuResult;
            if (stuMap != null)
            {
                veCommon.setStuId(Integer.parseInt(stuMap.get("id").toString()));
                pageNum = this.veDynamicMapper.getSumPage(veDynamicVo, veCommon);
            }
        }
        else if ((veCommon.getUserType().equals("4")) || (veCommon.getUserType().equals("3")))
        {
            VeCompanyUser veCompanyUser = this.veCompanyUserService.getCpInfoByUserId(veCommon.getUserId());
            veCommon.setCpId(veCompanyUser.getCpId());

            pageNum = this.veDynamicMapper.getSumPage(veDynamicVo, veCommon);
        }
        return pageNum;
    }

    public Result<?> getOneInfo(int id)
    {
        VePractice vePractice = (VePractice)this.vePracticeService.getById(Integer.valueOf(id));

        VeDynamicInfoVo veDynamicInfoVo = new VeDynamicInfoVo();
        if (vePractice == null) {
            return Result.error("系统异常");
        }
        Object stuResult = this.veCommonClient.getStudentById("09d5e1e7f9b049008eee645c783a1d67", vePractice.getStuUserId()).getResult();
        Map stuMap = (Map)stuResult;
        String s = JSON.toJSONString(stuMap);
        VeStudent veStudent = (VeStudent)JSON.parseObject(s, VeStudent.class);
        if (veStudent == null) {
            return Result.error("系统异常");
        }
        Object stuInfoResult = this.veCommonClient.getStudentInfoById("09d5e1e7f9b049008eee645c783a1d67", veStudent.getId()).getResult();
        Map stuInfoMap = (Map)stuInfoResult;
        String s1 = JSON.toJSONString(stuInfoMap);
        VeStudentInfo veStudentInfo = (VeStudentInfo)JSON.parseObject(s1, VeStudentInfo.class);
        if (veStudentInfo == null) {
            return Result.error("系统异常");
        }
        veDynamicInfoVo.setXm(veStudent.getXm());

        veDynamicInfoVo.setXh(veStudent.getXh());

        veDynamicInfoVo.setXslxdh(veStudentInfo.getXslxdh());

        veDynamicInfoVo.setJstxh(veStudentInfo.getJstxh());

        String specName = this.veDynamicMapper.getSpecName(veStudent.getSpecId());
        if ((specName != null) && (!"".equals(specName))) {
            veDynamicInfoVo.setSpec(specName);
        }
        String bjName = this.veDynamicMapper.getBjName(veStudent.getBjId());
        if ((bjName != null) && (!"".equals(bjName))) {
            veDynamicInfoVo.setBj(bjName);
        }
        veDynamicInfoVo.setJtdz(veStudentInfo.getJtdz());

        String sxqjzz = this.veDynamicMapper.getSxqjzz(vePractice.getStuUserId());
        if ((sxqjzz != null) && (!"".equals(sxqjzz))) {
            veDynamicInfoVo.setSxqjzz(sxqjzz);
        }
        veDynamicInfoVo.setJtlxdh(veStudentInfo.getJtlxdh());



        veDynamicInfoVo.setCpId(vePractice.getCpId());

        veDynamicInfoVo.setCpName(vePractice.getCpName());


        veDynamicInfoVo.setPostId(vePractice.getPostId());

        veDynamicInfoVo.setPostName(vePractice.getPostName());



        List<VeDynamicInfosVo> veDynamicInfosVosList = new ArrayList();

        List<VeDynamic> veDynamicList = this.veDynamicService.list();
        for (VeDynamic dynamic : veDynamicList) {
            if (dynamic.getStuUserId() == vePractice.getStuUserId())
            {
                VeDynamicInfosVo veDynamicInfosVo = new VeDynamicInfosVo();

                veDynamicInfosVo.setCreateTime(DateUtils.getDateToString(dynamic.getCreateTime()));

                veDynamicInfosVo.setCpName(dynamic.getCpName());

                veDynamicInfosVo.setPostName(dynamic.getPostName());
                if (dynamic.getCpId() != 0)
                {
                    VeCompany veCompany = (VeCompany)this.veCompanyService.getById(Integer.valueOf(dynamic.getCpId()));
                    if (veCompany != null) {
                        veDynamicInfosVo.setLinkman(veCompany.getLinkman());
                    }
                    if (veCompany != null) {
                        veDynamicInfosVo.setTel(veCompany.getTel());
                    }
                }
                veDynamicInfosVo.setRemark(dynamic.getRemark());
                veDynamicInfosVosList.add(veDynamicInfosVo);
            }
        }
        veDynamicInfoVo.setVeDynamicInfosVosList(veDynamicInfosVosList);

        return Result.OK(veDynamicInfoVo);
    }

    public void updateRemark(VeDynamicVo veDynamicVo)
    {
        VeDynamic veDynamics = (VeDynamic)this.veDynamicService.getById(Integer.valueOf(veDynamicVo.getId()));
        if ((veDynamics != null) && (!"".equals(veDynamics))) {
            this.veDynamicService.updateById(veDynamics);
        }
    }

    public Result<?> submitDynamic(VeDynamicAddVo veDynamicAddVo)
    {
        VePractice vePractice = (VePractice)this.vePracticeService.getById(Integer.valueOf(veDynamicAddVo.getId()));

        VeDynamic veDynamic = new VeDynamic();
        if ((vePractice == null) || ("".equals(vePractice))) {
            return Result.OK("提交id为空");
        }
        veDynamic.setStuUserId(vePractice.getStuUserId());

        veDynamic.setCreateTime(System.currentTimeMillis());

        Object teacherResult = this.veCommonClient.queryTeacherByUserId("09d5e1e7f9b049008eee645c783a1d67", veDynamicAddVo.getUserId()).getResult();
        Map teacherMap = (Map)teacherResult;
        if (teacherMap != null) {
            veDynamic.setCreateUserId(Integer.parseInt(teacherMap.get("id").toString()));
        }
        veDynamic.setPrcId(vePractice.getId());

        veDynamic.setPostId(veDynamicAddVo.getPostId());

        veDynamic.setPostName(veDynamicAddVo.getPostName());

        veDynamic.setCpId(veDynamicAddVo.getCpId());

        veDynamic.setCpName(veDynamicAddVo.getCpName());

        veDynamic.setRemark(veDynamicAddVo.getRemark());

        veDynamic.setTerminalId(1);

        this.veDynamicService.save(veDynamic);

        return Result.OK("成功");
    }
}
